<template>
  <div class="pxy20">
    <ElForm
      ref="icRefs"
      :model="articleContent"
      class="myfrom"
      :rules="caseICRules"
      label-width="80px"
      label-position="left"
    >
      <ElFormItem label="申诉原因" prop="title">
        <ElInput v-model="articleContent.title" placeholder="申诉原因" style="width: 400px" />
      </ElFormItem>

      <ElFormItem label="申诉类别" prop="type">
        <ElInput v-model="articleContent.type" placeholder="申诉类别" style="width: 400px" />
      </ElFormItem>
      <ElFormItem label="申诉结果" prop="appealResult">
        <ElInput v-model="articleContent.appealResult" placeholder="申诉结果" style="width: 400px" />
      </ElFormItem>

      <ElFormItem label="学校" prop="school">
        <ElInput v-model="articleContent.school" placeholder="学校" style="width: 400px" />
      </ElFormItem>

      <ElFormItem label="概述" prop="articleSummary">
        <ElInput v-model="articleContent.articleSummary" placeholder="概述" style="width: 400px" />
      </ElFormItem>

      <ElFormItem label="日期">
        <el-date-picker v-model="createDate" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" />
        <el-time-picker v-model="createTime" placeholder="选择时间" format="HH:mm:ss" />
      </ElFormItem>

      <ElFormItem label="封面" prop="image">
        <el-upload
          class="avatar-uploader"
          action="https://api.ukshuxi.com/api/file/uploadFile"
          :show-file-list="false"
          :on-success="uploadSuccess"
        >
          <img v-if="articleContent.image" :src="baseUrl+articleContent.image" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
      </ElFormItem>

      <ElFormItem label="正文" prop="content"><br>
        <editorVue v-model="articleContent.content" />
      </ElFormItem>
    </ElForm>
    <br>
    <ElButton type="primary" @click="submitBtn">发布</ElButton>
  </div>
</template>
<script>
import editorVue from '@/views/article/editor.vue'
import { caseICRules } from '@/utils/formVerify'
import { CASE_ADD, CASE_EDIT } from '@/api/ACAI/case'

export default {
  name: 'ArticleEdit',
  components: { editorVue },
  data() {
    return {
      articleContent: {
        title: '',
        type: '',
        articleSummary: '',
        content: '',
        school: '',
        image: '',
        caseCreateTime: '',
        appealResult: ''
      },
      createDate: '',
      createTime: '',
      caseICRules,
      baseUrl: 'https://api.ukshuxi.com'
    }
  },
  mounted() {
    const row = this.$route.params
    if (Object.keys(row).length !== 0) {
      this.articleContent = row
      this.createDate = this.articleContent.caseCreateTime.slice(0, 11)
      this.createTime = new Date(this.articleContent.caseCreateTime)
    }
  },
  methods: {
    submitBtn() {
      if (!this.createDate || !this.createTime) {
        this.$message.warning('请选择日期和时间')
      }
      this.$refs['icRefs'].validate((i) => {
        this.articleContent.caseCreateTime = this.createDate + ' ' + this.createTime.getHours() + ':' + this.createTime.getMinutes()
        if (i) {
          this.articleContent.id
            ? CASE_EDIT(this.articleContent).then((res) => {
              console.log('修改案例', res)
              this.apiRes(res)
            })
            : CASE_ADD(this.articleContent).then((res) => {
              console.log('增加案例', res)
              this.apiRes(res)
            })
        }
      })
    },

    uploadSuccess(res, file, fileList) {
      console.log(res)
      this.articleContent.image =
        '/drdImg/' + res.data.fileName
      this.$message({
        message:
          +res.code === 200 ? '封面上传成功!' : '封面上传失败，请联系系统管理员!',
        type: +res.code === 200 ? 'success' : 'error'
      })
    },

    apiRes(res) {
      const i = +res['result'] === 0

      this.$message({
        message: i
          ? this.articleContent.id
            ? '案例更新成功!'
            : '案例增加成功！'
          : '案例编辑失败!' + res.msg,
        type: i ? 'success' : 'error'
      })
      i ? this.$refs['icRefs'].resetFields() : ''
      this.$router.push('/appeal/case')
    }
  }
}
</script>
<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.myfrom {
}
</style>
